package com.itheima.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.resource.MessageConstant;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import com.itheima.utils.UploadUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.ibatis.ognl.MapElementsAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetMealService setMealService;
    /**
     * 上传图片到七牛云
     * @param file
     * @return
     */
    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("imgFile")MultipartFile file) {
        try {
            if (!file.isEmpty()) {
                //获取文件上传名称
                String originalFilename = file.getOriginalFilename();
                //获取uuid唯一名称
                String fileName = UploadUtils.generateRandonFileName(originalFilename);
                //将图片上传到七牛云
                QiniuUtils.upload2Qiniu(file.getBytes(), fileName);
                //将要上传到七牛云的图片存放到redis中
                jedisPool.getResource().sadd("SETMEAL_PIC_RESOURCES",fileName);
                return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
            }else {
                return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }


    /**
     * 添加套餐
     * @param map
     * @return
     */
    @RequestMapping("/addsetmeal.do")
    public Result addGroup(@RequestBody Map<String,Object> map){//使用map接收参数
        try {
            //使用fastjson封装数据，自动转换到实体类对象
            Setmeal setmeal=
                    JSONObject.parseObject(map.get("setmeal").toString(),Setmeal.class);


            Integer[] checkgroupIds=JSONObject.parseObject(map.get("ids").toString(),Integer[].class);

            System.out.println(setmeal);
            System.out.println(checkgroupIds);

            setMealService.addSetMeal(setmeal,checkgroupIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }

    }

    /**
     * 检查组条件查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        try {
            Map map = setMealService.findPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }


    }

    /**
     * 根据套餐的id获取对应的检查组的id
     * @param id
     * @return
     */
    @RequestMapping("/findCheckgroupIdsBySetMealId.do")
    public Result findCheckgroupIdsBySetMealId(@RequestParam("id") Integer id){
        try {
            Integer[] checkgroupIds=setMealService.findCheckgroupIdsBySetMealId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_SUCCESS);
        }

    }

    /**
     * 获取id对饮套餐数据
     * @param id
     * @return
     */
    @RequestMapping("/findAllSetMeal.do")
    public Result findAllSetMeal(@RequestParam("id") Integer id){
        try {
            Setmeal setmeal=setMealService.findAllSetMeal(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 编辑套餐
     * @return
     */
    @RequestMapping("/editsetmeal.do")
    public Result editsetmeal(@RequestBody Map<String,Object> map){
        try {
            //使用fastjson封装数据，自动转换到实体类对象
            Setmeal setmeal=
                    JSONObject.parseObject(map.get("setmeal").toString(),Setmeal.class);


            Integer[] checkgroupIds=JSONObject.parseObject(map.get("ids").toString(),Integer[].class);

            System.out.println(setmeal);
            System.out.println(checkgroupIds);

            setMealService.editsetmeal(setmeal,checkgroupIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

}
